﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.ClickCricket.bll;

/// <summary>
/// Summary description for CountryLinqProvider
/// </summary>
/// 

namespace com.ClickCricket.dal
{
    public class CountryLinqProvider : ICountryProvider
    {
        public CountryLinqProvider()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Country getCountry(int CountryId)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                var country = from aCountry in dc.CountryTables
                              where aCountry.CountryID == CountryId
                              select aCountry;
                if (country == null)
                {
                    return null;
                }
                if (country.Count<Entity.CountryTable>() == 1)
                {
                    return extractCountry(country);
                }
                throw new Exception("Invalid Country Id " + CountryId + " !");
            }
        }

        public Country getRandomCountry()
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                System.Data.Linq.Table<Entity.CountryTable> countriesTable = dc.GetTable<Entity.CountryTable>();

                int numberOfCountries = countriesTable.Count<Entity.CountryTable>();
                List<int> CountryIds = new List<int>();

                foreach (var varplayer in countriesTable)
                {
                    CountryIds.Add(varplayer.CountryID);
                }
                Random rand = new Random();

                int randomCountryId = CountryIds[rand.Next(0, CountryIds.Count)];

                var country = from aCountry in dc.CountryTables
                              where aCountry.CountryID == randomCountryId
                              select aCountry;
                if (country == null)
                {
                    return null;
                }
                if (country.Count<Entity.CountryTable>() == 1)
                {
                    return extractCountry(country);
                }
                throw new Exception("Invalid Country Id " + randomCountryId + " !");
            }
        }

        public List<Country> getCountries()
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Country> _CountryList = new List<Country>();
                var countries = from aCountry in dc.CountryTables
                              select aCountry;
                if (countries == null)
                {
                    return null;
                }
                foreach (var country in countries)
                {
                    _CountryList.Add(extractCountry(country));
                }
                return _CountryList;
            }
        }



        public List<Country> getCountry()
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Country> _CountryList = new List<Country>();
                var countries = from aCountry in dc.CountryTables
                              select aCountry;
                if (countries == null)
                {
                    return null;
                }
                foreach (var country in countries)
                {
                    _CountryList.Add(extractCountry(country));
                }
                return _CountryList;
            }
        }
        private Country extractCountry(Entity.CountryTable country)
        {
            string imagepath = "~/" + country.CountryImage;
            Country aCountry = new Country(
                country.CountryID,
                country.CountryName,
                imagepath);
            return aCountry;
        }

        private Country extractCountry(IQueryable<Entity.CountryTable> country)
        {
            string imagepath = "~/" + country.Single<Entity.CountryTable>().CountryImage;
            Country aCountry = new Country(
                country.Single<Entity.CountryTable>().CountryID,
                country.Single<Entity.CountryTable>().CountryName,
                imagepath);
            return aCountry;
        }
    }
}
